
********************************************************************************
********************************************************************************
************ Relax IND Model Table for Online Appendix  (Table S.1) ************
********************************************************************************
********************************************************************************
cap mkdir `"$output/model_estimation"'
cap mkdir `"$output/model_estimation/relax_SUF"'
cap mkdir `"$output/model_estimation/relax_IND"'

eststo clear 

//load the tau estimates for Medicaid 
est use `"$output/model_estimation/relax_IND/parameters/taurf_Medicaid.ster"'
eststo taurf_caid

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicaid_pooledreasons.ster"'
eststo tau_caid_pr

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicaid_byreasons_allgroups.ster"'
eststo tau_caid_byr_ag 

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicaid_byreasons_smallgroups.ster"'
eststo tau_caid_byr_sg 

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicaid_byreasons_largegroups.ster"'
eststo tau_caid_byr_lg

//load the tau estimates for Medicare 
est use `"$output/model_estimation/relax_IND/parameters/taurf_Medicare.ster"'
eststo taurf_care

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicare_pooledreasons.ster"'
eststo tau_care_pr

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicare_byreasons_allgroups.ster"'
eststo tau_care_byr_ag 

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicare_byreasons_smallgroups.ster"'
eststo tau_care_byr_sg 

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicare_byreasons_largegroups.ster"'
eststo tau_care_byr_lg

//load the tau estimates for Commercial 
est use `"$output/model_estimation/relax_IND/parameters/taurf_Commercial.ster"'
eststo taurf_com

est use `"$output/model_estimation/relax_IND/parameters/tau_Commercial_pooledreasons.ster"'
eststo tau_com_pr

est use `"$output/model_estimation/relax_IND/parameters/tau_Commercial_byreasons_allgroups.ster"'
eststo tau_com_byr_ag 

est use `"$output/model_estimation/relax_IND/parameters/tau_Commercial_byreasons_smallgroups.ster"'
eststo tau_com_byr_sg 

est use `"$output/model_estimation/relax_IND/parameters/tau_Commercial_byreasons_largegroups.ster"'
eststo tau_com_byr_lg


//set cd to avoid conflict on BEA server 
cd `"$output/model_estimation/relax_IND"'

		#delimit ;
		esttab taurf_caid tau_caid_pr tau_caid_byr_ag tau_caid_byr_sg tau_caid_byr_lg
			   taurf_care tau_care_pr tau_care_byr_ag tau_care_byr_sg tau_care_byr_lg 
			   taurf_com tau_com_pr tau_com_byr_ag tau_com_byr_sg tau_com_byr_lg
			   using "table5_row1.tex", replace substitute(_ \_)
		nonumbers se b(3) se(4) noobs nonotes nostar
		mtitles("\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{Small \\ group.}" "\shortstack{Large \\ group.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{Small \\ group}" "\shortstack{Large \\ group}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{Small \\ group}" "\shortstack{Large \\ group}") booktabs mgroups("Medicaid" "Medicare" "Commercial", pattern(1 0 0 0 0 1 0 0 0 0 1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) keep(_cons) 
		coeflabels(_cons "Average $\tau$");
		
		#delimit cr

eststo clear 


//load the CIP estimates for Medicaid 
est use `"$output/model_estimation/relax_IND/parameters/lost_revenue_Medicaid.ster"'
eststo lr_caid

est use `"$output/model_estimation/relax_IND/parameters/CIP_Medicaid_pooledreasons.ster"'
eststo CIP_caid_pr

est use `"$output/model_estimation/relax_IND/parameters/CIP_Medicaid_byreasons_allgroups.ster"'
eststo CIP_caid_byr_ag 

est use `"$output/model_estimation/relax_IND/parameters/CIP_Medicaid_byreasons_smallgroups.ster"'
eststo CIP_caid_byr_sg 

est use `"$output/model_estimation/relax_IND/parameters/CIP_Medicaid_byreasons_largegroups.ster"'
eststo CIP_caid_byr_lg

//load the CIP estimates for Medicare 
est use `"$output/model_estimation/relax_IND/parameters/lost_revenue_Medicare.ster"'
eststo lr_care

est use `"$output/model_estimation/relax_IND/parameters/CIP_Medicare_pooledreasons.ster"'
eststo CIP_care_pr

est use `"$output/model_estimation/relax_IND/parameters/CIP_Medicare_byreasons_allgroups.ster"'
eststo CIP_care_byr_ag 

est use `"$output/model_estimation/relax_IND/parameters/CIP_Medicare_byreasons_smallgroups.ster"'
eststo CIP_care_byr_sg 

est use `"$output/model_estimation/relax_IND/parameters/CIP_Medicare_byreasons_largegroups.ster"'
eststo CIP_care_byr_lg

//load the CIP estimates for Commercial 
est use `"$output/model_estimation/relax_IND/parameters/lost_revenue_Commercial.ster"'
eststo lr_com

est use `"$output/model_estimation/relax_IND/parameters/CIP_Commercial_pooledreasons.ster"'
eststo CIP_com_pr

est use `"$output/model_estimation/relax_IND/parameters/CIP_Commercial_byreasons_allgroups.ster"'
eststo CIP_com_byr_ag 

est use `"$output/model_estimation/relax_IND/parameters/CIP_Commercial_byreasons_smallgroups.ster"'
eststo CIP_com_byr_sg 

est use `"$output/model_estimation/relax_IND/parameters/CIP_Commercial_byreasons_largegroups.ster"'
eststo CIP_com_byr_lg

//set cd to avoid conflict on BEA server 
cd `"$output/model_estimation/relax_IND"'

		#delimit ;
		esttab lr_caid CIP_caid_pr CIP_caid_byr_ag CIP_caid_byr_sg CIP_caid_byr_lg
			   lr_care CIP_care_pr CIP_care_byr_ag CIP_care_byr_sg CIP_care_byr_lg 
			   lr_com CIP_com_pr CIP_com_byr_ag CIP_com_byr_sg CIP_com_byr_lg
			   using "table5_row2.tex", replace substitute(_ \_)
		nonumbers se b(2) se(3) nostar noobs nonotes 
		mtitles("\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{Small \\ group.}" "\shortstack{Large \\ group.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{Small \\ group.}" "\shortstack{Large \\ group.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{Small \\ group.}" "\shortstack{Large \\ group.}") booktabs mgroups("Medicaid" "Medicare" "Commercial", pattern(1 0 0 0 0 1 0 0 0 0 1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) keep(_cons) 
		coeflabels(_cons "Average CIP");
		
		#delimit cr
		

		
		
		
		
****************************************************
***************** Row 3 ****************************
****************************************************
		
clear all		
set obs 6000
gen y = 1
gen x = 5
gen z = 2

reg y x z, nocons
eststo placeholder 		
	
gen y1 = 1
gen x1 = 5
gen z1 = 2

reg y1 x1 z1, nocons
eststo placeholder1	


local rename _cons taucoef x1 taucoef


//load Medicaid results for model esimtations, no reason, no size 
use `"$output/model_estimation/relax_IND/estimations_nosize_noreason_bin.dta"', clear 
est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicaid_allreasons_allgroups.ster"'
//determine esitmates to be used 
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p99)
keep if pay_type_pooled == 2


//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row3_col2



//load Medicare results for model esimtations, no reason, no size 
use `"$output/model_estimation/relax_IND/estimations_nosize_noreason_bin.dta"', clear 
est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicare_allreasons_allgroups.ster"'

//determine esitmates to be used 
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p99)
keep if pay_type_pooled == 1


//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row3_col7


//load Commercial results for model esimtations, no reason, no size 
use `"$output/model_estimation/relax_IND/estimations_nosize_noreason_bin.dta"', clear 
est use `"$output/model_estimation/relax_IND/parameters/model_est_Commercial_allreasons_allgroups.ster"'

//determine esitmates to be used 
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p99)
keep if pay_type_pooled == 3


//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row3_col12


//set cd to avoid conflict on BEA server 
cd `"$output/model_estimation/relax_IND"'

local rename _cons taucoef z taucoef
	#delimit;

	esttab placeholder row3_col2 placeholder1 placeholder1 placeholder1 placeholder row3_col7 placeholder1 placeholder1 placeholder1 
	placeholder row3_col12 placeholder1 placeholder1 placeholder1 using "table5_row3.tex", 
	rename(`rename') keep(taucoef) replace 
	coeflabels(taucoef "$\mu^{0}$, all")
	not stats(serror, labels(" ")) nostar b(2) noobs nolines nonumber nomtitles;

	#delimit cr


	
****************************************************
*********** MODEL ESTIMATES BY REASON **************
****************************************************
local reasons `""administrative" "contractual" "coverage" "duplicate" "information""'
	
foreach r of local reasons {
	
	if "`r'" == "administrative" {
		
		local row = 4
		local tlab "Admin."
	}
	
	else if "`r'" == "contractual" {
		
		local row = 5
		local tlab "Contr."
	}
	
	else if "`r'" == "coverage" {
		
		local row = 6
		local tlab "Cov."
	}
	
	else if "`r'" == "duplicate" {
		
		local row = 7
		local tlab "Dup."
	}
	
	else if "`r'" == "information" {
		
		local row = 8
		local tlab "Info."
	}
****************************************************
************** Row 4, 5, 6, 7, 8 *****************
****************************************************
clear all 

//crate placeholder 		
clear all		
set obs 6000
gen y = 1
gen x = 5
gen z = 2

reg y x z, nocons
eststo placeholder 		

//local for renaming coefficients 
local rename_row _cons taucoef

*****************************************
********* Column 3 
//load Medicaid results for model esimtations, reason, no size 
use `"$output/model_estimation/relax_IND/estimations_nosize_bin.dta"', clear 
est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicaid_`r'_allgroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 2
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col3


*****************************************
********* Column 4
//load Medicaid results for model esimtations, reason, size 
use `"$output/model_estimation/relax_IND/estimations_bin.dta"', clear 
keep if size_indicator == 1 
est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicaid_`r'_smallgroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 2
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col4

*****************************************
********* Column 5
//load Medicaid results for model esimtations, reason, size 
use `"$output/model_estimation/relax_IND/estimations_bin.dta"', clear 
keep if size_indicator == 2
est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicaid_`r'_largegroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 2
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col5



*****************************************
********* Column 8
//load Medicare results for model esimtations, reason, no size 
use `"$output/model_estimation/relax_IND/estimations_nosize_bin.dta"', clear 
est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicare_`r'_allgroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 1
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col8


*****************************************
********* Column 9
//load Medicare results for model esimtations, reason, size 
use `"$output/model_estimation/relax_IND/estimations_bin.dta"', clear 
keep if size_indicator == 1 
est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicare_`r'_smallgroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 1
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col9

*****************************************
********* Column 10
//load Medicaid results for model esimtations, reason, size 
use `"$output/model_estimation/relax_IND/estimations_bin.dta"', clear 
keep if size_indicator == 2
est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicare_`r'_largegroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 1
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col10



*****************************************
********* Column 13
//load Commercial results for model esimtations, reason, no size 
use `"$output/model_estimation/relax_IND/estimations_nosize_bin.dta"', clear 
est use `"$output/model_estimation/relax_IND/parameters/model_est_Commercial_`r'_allgroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 3
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col13


*****************************************
********* Column 14
//load Medicaid results for model esimtations, reason, size 
use `"$output/model_estimation/relax_IND/estimations_bin.dta"', clear 
keep if size_indicator == 1 
est use `"$output/model_estimation/relax_IND/parameters/model_est_Commercial_`r'_smallgroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 3
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col14

*****************************************
********* Column 15
//load Medicaid results for model esimtations, reason, size 
use `"$output/model_estimation/relax_IND/estimations_bin.dta"', clear 
keep if size_indicator == 2
est use `"$output/model_estimation/relax_IND/parameters/model_est_Commercial_`r'_largegroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 3
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col15

//set cd to avoid conflict on BEA server 
cd `"$output/model_estimation/relax_IND"'

// tabulate the row 
	#delimit;

	esttab placeholder placeholder row`row'_col3 row`row'_col4 row`row'_col5 placeholder placeholder row`row'_col8 row`row'_col9 row`row'_col10 
	placeholder placeholder row`row'_col13 row`row'_col14 row`row'_col15 using "table5_row`row'.tex", 
	rename(`rename_row') keep(taucoef) 
	coeflabels(taucoef "$\mu^{0}$, `tlab' $\rho$")
	replace 
	not stats(serror, labels(" ")) nostar b(2) noobs nolines nonumber nomtitles;

	#delimit cr


}






***************************************************
************** Rows 9 and 10 *********************
***************************************************
eststo clear 
clear all 
set obs 10 
//placeholder for column 1 
gen placeholder_col_nomodel = 0
estpost tabstat placeholder_col_nomodel
eststo col_nomodelNLL



//load the tau estimates for Medicaid 
est use `"$output/model_estimation/relax_IND/parameters/taurf_Medicaid.ster"'
gen caid_nom_N = e(N)

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicaid_pooledreasons.ster"'
gen caid_pr_N = e(N)

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicaid_byreasons_allgroups.ster"'
gen caid_all_N = e(N)

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicaid_byreasons_smallgroups.ster"'
gen caid_small_N = e(N)

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicaid_byreasons_largegroups.ster"'
gen caid_large_N = e(N)

//load the tau estimates for Medicare 
est use `"$output/model_estimation/relax_IND/parameters/taurf_Medicare.ster"'
gen care_nom_N = e(N)

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicare_pooledreasons.ster"'
gen care_pr_N = e(N)

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicare_byreasons_allgroups.ster"'
gen care_all_N = e(N)

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicare_byreasons_smallgroups.ster"'
gen care_small_N = e(N)

est use `"$output/model_estimation/relax_IND/parameters/tau_Medicare_byreasons_largegroups.ster"'
gen care_large_N = e(N)

//load the tau estimates for Commercial 
est use `"$output/model_estimation/relax_IND/parameters/taurf_Commercial.ster"'
gen com_nom_N = e(N)

est use `"$output/model_estimation/relax_IND/parameters/tau_Commercial_pooledreasons.ster"'
gen com_pr_N = e(N)

est use `"$output/model_estimation/relax_IND/parameters/tau_Commercial_byreasons_allgroups.ster"'
gen com_all_N = e(N)

est use `"$output/model_estimation/relax_IND/parameters/tau_Commercial_byreasons_smallgroups.ster"'
gen com_small_N = e(N)

est use `"$output/model_estimation/relax_IND/parameters/tau_Commercial_byreasons_largegroups.ster"'
gen com_large_N = e(N)




//load the tau estimates for Medicaid -- pooled reason 
est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicaid_allreasons_allgroups.ster"'
gen tau_caid_pr_LL = e(ll)

estpost tabstat caid_nom_N
eststo col1N
estpost tabstat caid_pr_N
eststo col2N

estpost tabstat tau_caid_pr_LL
eststo col2LL

local version `""all" "small" "large""'


//Medicaid Estimations by reason 
foreach v of local version {
	
	//load the tau estimates for Medicaid -- by reason, all groups 
	est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicaid_administrative_`v'groups.ster"'
	gen admin_caid_`v'_LL = e(ll)
	
	est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicaid_contractual_`v'groups.ster"'
	gen contr_caid_`v'_LL = e(ll)
	
	est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicaid_coverage_`v'groups.ster"'
	gen cov_caid_`v'_LL = e(ll)
	
	est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicaid_duplicate_`v'groups.ster"'
	gen dup_caid_`v'_LL = e(ll)
	
	est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicaid_information_`v'groups.ster"'
	gen info_caid_`v'_LL = e(ll)
	
	gen total_caid_`v'_LL = admin_caid_`v'_LL + contr_caid_`v'_LL + cov_caid_`v'_LL + dup_caid_`v'_LL + info_caid_`v'_LL

}
 
estpost tabstat caid_all_N
eststo col3N

estpost tabstat caid_small_N
eststo col4N

estpost tabstat caid_large_N
eststo col5N

estpost tabstat total_caid_all_LL
eststo col3LL

estpost tabstat total_caid_small_LL
eststo col4LL

estpost tabstat total_caid_large_LL
eststo col5LL

//load the tau estimates for Medicare -- pooled reason 
est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicare_allreasons_allgroups.ster"'
gen tau_care_pr_LL = e(ll)
 
estpost tabstat care_nom_N
eststo col6N

estpost tabstat care_pr_N
eststo col7N

estpost tabstat tau_care_pr_LL
eststo col7LL

//Medicare Estimations 
foreach v of local version {
	
	//load the tau estimates for Medicare -- by reason, all groups 
	est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicare_administrative_`v'groups.ster"'
	gen admin_care_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicare_contractual_`v'groups.ster"'
	gen contr_care_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicare_coverage_`v'groups.ster"'
	gen cov_care_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicare_duplicate_`v'groups.ster"'
	gen dup_care_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_IND/parameters/model_est_Medicare_information_`v'groups.ster"'
	gen info_care_`v'_LL = e(ll)

	gen total_care_`v'_LL = admin_care_`v'_LL + contr_care_`v'_LL + cov_care_`v'_LL + dup_care_`v'_LL + info_care_`v'_LL

}
 
estpost tabstat care_all_N
eststo col8N

estpost tabstat care_small_N
eststo col9N

estpost tabstat care_large_N
eststo col10N

estpost tabstat total_care_all_LL
eststo col8LL

estpost tabstat total_care_small_LL
eststo col9LL

estpost tabstat total_care_large_LL
eststo col10LL

//load the tau estimates for Commercial -- pooled reason 
est use `"$output/model_estimation/relax_IND/parameters/model_est_Commercial_allreasons_allgroups.ster"'
gen tau_com_pr_LL = e(ll)

estpost tabstat com_nom_N
eststo col11N

estpost tabstat com_pr_N
eststo col12N

estpost tabstat tau_com_pr_LL
eststo col12LL

//Commercial Estimations 
foreach v of local version {
	
	//load the tau estimates for Commercial -- by reason, all groups 
	est use `"$output/model_estimation/relax_IND/parameters/model_est_Commercial_administrative_`v'groups.ster"'
	gen admin_com_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_IND/parameters/model_est_Commercial_contractual_`v'groups.ster"'
	gen contr_com_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_IND/parameters/model_est_Commercial_coverage_`v'groups.ster"'
	gen cov_com_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_IND/parameters/model_est_Commercial_duplicate_`v'groups.ster"'
	gen dup_com_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_IND/parameters/model_est_Commercial_information_`v'groups.ster"'
	gen info_com_`v'_LL = e(ll)

	gen total_com_`v'_LL = admin_com_`v'_LL + contr_com_`v'_LL + cov_com_`v'_LL + dup_com_`v'_LL + info_com_`v'_LL

}
 
estpost tabstat com_all_N
eststo col13N

estpost tabstat com_small_N
eststo col14N

estpost tabstat com_large_N
eststo col15N

estpost tabstat total_com_all_LL
eststo col13LL

estpost tabstat total_com_small_LL
eststo col14LL

estpost tabstat total_com_large_LL
eststo col15LL

//set cd to avoid conflict on BEA server 
cd `"$output/model_estimation/relax_IND"'

#delimit ;
	esttab col1N col2N col3N col4N col5N col6N col7N col8N col9N col10N col11N col12N col13N col14N col15N 
			using "table5_row9.tex" , cells("mean(fmt(%20.0fc))") 
	rename(caid_nom_N "Number of visits" caid_pr_N "Number of visits" caid_all_N "Number of visits" caid_small_N "Number of visits" caid_large_N "Number of visits" 
			care_nom_N "Number of visits" care_pr_N "Number of visits" care_all_N "Number of visits" care_small_N "Number of visits" care_large_N "Number of visits"
		   com_nom_N "Number of visits" com_pr_N "Number of visits" com_all_N "Number of visits" com_small_N "Number of visits" com_large_N "Number of visits")
	unstack nonumbers booktabs replace substitute(_ \_) noobs;
#delimit cr


#delimit ;
	esttab col_nomodelNLL col2LL col3LL col4LL col5LL col_nomodelNLL col7LL col8LL col9LL col10LL col_nomodelNLL col12LL col13LL col14LL col15LL 
			using "table5_row10.tex", cells("mean(fmt(%20.0fc))") 
	rename(placeholder_col_nomodel "Log Likelihood" tau_caid_pr_LL "Log Likelihood" total_caid_all_LL "Log Likelihood" total_caid_small_LL "Log Likelihood" total_caid_large_LL "Log Likelihood"
		   tau_care_pr_LL "Log Likelihood" total_care_all_LL "Log Likelihood" total_care_small_LL "Log Likelihood" total_care_large_LL "Log Likelihood"
		   tau_com_pr_LL "Log Likelihood" total_com_all_LL "Log Likelihood" total_com_small_LL "Log Likelihood" total_com_large_LL "Log Likelihood")
	unstack nonumbers booktabs replace substitute(_ \_) noobs;
#delimit cr









//Combine the tables into one tables with three panels 
cap prog drop panelcombine
prog define panelcombine
qui {
syntax, use(str asis) paneltitles(str asis) columncount(integer) save(str asis) [CLEANup]
preserve

tokenize `"`paneltitles'"'
//read in loop
local num 1
while "``num''"~="" {
local panel`num'title="``num''"
local num=`num'+1
}


tokenize `use'
//read in loop
local num 1
while "``num''"~="" {
tempfile temp`num'
insheet using "``num''", clear
save `temp`num''
local max = `num'
local num=`num'+1
}

//conditional processing loop
local num 1
while "``num''"~="" {
local panellabel : word `num' of `c(alpha)'
use `temp`num'', clear
	if `num'==1 { //process first panel -- clip bottom
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="\end{tabular}" | v1=="}"
	replace v1 = "[1em]" if v1=="\bottomrule" 
	}
	
	else if `num'==8 { //process final panel -- clip top
	//process header to drop everything until first hline
	drop if v1=="\begin{tabular}{l*{15}{c}}"
	drop if v1=="[1em]" 
	replace v1 = "\midrule" if v1=="\end{tabular}"
		}
	
	if `num'==9 { //process first panel -- clip bottom
	g temp = (v1 == "\midrule")
	replace temp = temp+temp[_n-1] if _n>1
	drop if temp==0
	drop temp
	
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="}" | v1=="\end{tabular}" | v1=="\bottomrule" | v1=="\midrule" 
	}
		
	else if `num'==`max' { //process final panel -- clip top
	//process header to drop everything until first hline
	g temp = (v1 == "\midrule")
	replace temp = temp+temp[_n-1] if _n>1
	drop if temp==0
	drop temp
	drop if v1=="\begin{tabular}{l*{15}{c}}" | v1=="\midrule" 
	replace v1="[0.5em]" if v1=="[1em]"
			}

		
	else if `num'==2 { //process middle panels -- clip top and bottom
	//process header to drop everything until first hline
	g temp = (v1 == "\midrule")
	replace temp = temp+temp[_n-1] if _n>1
	drop if temp==0
	drop temp
	
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="}" | v1=="\end{tabular}"
	//replace v1="[1em]" if v1=="\end{tabular}"
	drop if v1=="\midrule"
	replace v1 = "\midrule \\" if v1=="\bottomrule"
	}
	
	else if `num'== 3 { //process middle panels -- clip top and bottom
	//process header to drop everything until first hline
	
	// drop if v1=="\begin{tabular}{l*{15}{c}}"
	drop if v1=="\begin{tabular}{l*{15}{c}}"
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="\end{tabular}" | v1=="}"
	replace v1 = "" if v1=="\bottomrule"
	drop if v1=="\bottomrule"
	drop if v1=="\midrule"
	}

	forval cline = 4(1)7 {
		else if `num'== `cline' { //process middle panels -- clip top and bottom
		//process header to drop everything until first hline
		
		// drop if v1=="\begin{tabular}{l*{15}{c}}"
		replace v1="[1em]" if v1=="\begin{tabular}{l*{15}{c}}"
		drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
		drop if v1=="\end{tabular}" | v1=="}"
		replace v1 = "" if v1=="\bottomrule"
		drop if v1=="\bottomrule"
		drop if v1=="\midrule"
		}
	}
	save `temp`num'', replace
local num=`num'+1
}

use `temp1',clear
local num 2
while "``num''"~="" {
append using `temp`num''
local num=`num'+1
}

outsheet using `save', noname replace noquote


	if "`cleanup'"!="" { //erasure loop
	tokenize `use'
	local num 1
		while "``num''"~="" {
		erase "``num''"
		local num=`num'+1
		}
	}

restore
}
end

cd `"$output/model_estimation/relax_IND"'
panelcombine, use(table5_row1.tex table5_row2.tex table5_row3.tex table5_row4.tex table5_row5.tex table5_row6.tex table5_row7.tex table5_row8.tex table5_row9.tex table5_row10.tex)  columncount(15) paneltitles("") save(`"$online_appendix/model_relax_IND.tex"') cleanup 








********************************************************************************
********************************************************************************
*********** Relax SUF Model Table for Online Appendix  (Table S.2) *************
********************************************************************************
********************************************************************************


eststo clear 

//load the tau estimates for Medicaid 
est use `"$output/model_estimation/relax_SUF/parameters/taurf_Medicaid.ster"'
eststo taurf_caid

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicaid_pooledreasons.ster"'
eststo tau_caid_pr

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicaid_byreasons_allgroups.ster"'
eststo tau_caid_byr_ag 

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicaid_byreasons_smallgroups.ster"'
eststo tau_caid_byr_sg 

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicaid_byreasons_largegroups.ster"'
eststo tau_caid_byr_lg

//load the tau estimates for Medicare 
est use `"$output/model_estimation/relax_SUF/parameters/taurf_Medicare.ster"'
eststo taurf_care

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicare_pooledreasons.ster"'
eststo tau_care_pr

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicare_byreasons_allgroups.ster"'
eststo tau_care_byr_ag 

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicare_byreasons_smallgroups.ster"'
eststo tau_care_byr_sg 

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicare_byreasons_largegroups.ster"'
eststo tau_care_byr_lg

//load the tau estimates for Commercial 
est use `"$output/model_estimation/relax_SUF/parameters/taurf_Commercial.ster"'
eststo taurf_com

est use `"$output/model_estimation/relax_SUF/parameters/tau_Commercial_pooledreasons.ster"'
eststo tau_com_pr

est use `"$output/model_estimation/relax_SUF/parameters/tau_Commercial_byreasons_allgroups.ster"'
eststo tau_com_byr_ag 

est use `"$output/model_estimation/relax_SUF/parameters/tau_Commercial_byreasons_smallgroups.ster"'
eststo tau_com_byr_sg 

est use `"$output/model_estimation/relax_SUF/parameters/tau_Commercial_byreasons_largegroups.ster"'
eststo tau_com_byr_lg

//set cd to avoid conflict on BEA server 
cd `"$output/model_estimation/relax_SUF"'

		#delimit ;
		esttab taurf_caid tau_caid_pr tau_caid_byr_ag tau_caid_byr_sg tau_caid_byr_lg
			   taurf_care tau_care_pr tau_care_byr_ag tau_care_byr_sg tau_care_byr_lg 
			   taurf_com tau_com_pr tau_com_byr_ag tau_com_byr_sg tau_com_byr_lg
			   using "table5_row1.tex", replace substitute(_ \_)
		nonumbers se b(3) se(4) noobs nonotes nostar
		mtitles("\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{Small \\ group.}" "\shortstack{Large \\ group.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{Small \\ group}" "\shortstack{Large \\ group}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{Small \\ group}" "\shortstack{Large \\ group}") booktabs mgroups("Medicaid" "Medicare" "Commercial", pattern(1 0 0 0 0 1 0 0 0 0 1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) keep(_cons) 
		coeflabels(_cons "Average $\tau$");
		
		#delimit cr

eststo clear 


//load the CIP estimates for Medicaid 
est use `"$output/model_estimation/relax_SUF/parameters/lost_revenue_Medicaid.ster"'
eststo lr_caid

est use `"$output/model_estimation/relax_SUF/parameters/CIP_Medicaid_pooledreasons.ster"'
eststo CIP_caid_pr

est use `"$output/model_estimation/relax_SUF/parameters/CIP_Medicaid_byreasons_allgroups.ster"'
eststo CIP_caid_byr_ag 

est use `"$output/model_estimation/relax_SUF/parameters/CIP_Medicaid_byreasons_smallgroups.ster"'
eststo CIP_caid_byr_sg 

est use `"$output/model_estimation/relax_SUF/parameters/CIP_Medicaid_byreasons_largegroups.ster"'
eststo CIP_caid_byr_lg

//load the CIP estimates for Medicare 
est use `"$output/model_estimation/relax_SUF/parameters/lost_revenue_Medicare.ster"'
eststo lr_care

est use `"$output/model_estimation/relax_SUF/parameters/CIP_Medicare_pooledreasons.ster"'
eststo CIP_care_pr

est use `"$output/model_estimation/relax_SUF/parameters/CIP_Medicare_byreasons_allgroups.ster"'
eststo CIP_care_byr_ag 

est use `"$output/model_estimation/relax_SUF/parameters/CIP_Medicare_byreasons_smallgroups.ster"'
eststo CIP_care_byr_sg 

est use `"$output/model_estimation/relax_SUF/parameters/CIP_Medicare_byreasons_largegroups.ster"'
eststo CIP_care_byr_lg

//load the CIP estimates for Commercial 
est use `"$output/model_estimation/relax_SUF/parameters/lost_revenue_Commercial.ster"'
eststo lr_com

est use `"$output/model_estimation/relax_SUF/parameters/CIP_Commercial_pooledreasons.ster"'
eststo CIP_com_pr

est use `"$output/model_estimation/relax_SUF/parameters/CIP_Commercial_byreasons_allgroups.ster"'
eststo CIP_com_byr_ag 

est use `"$output/model_estimation/relax_SUF/parameters/CIP_Commercial_byreasons_smallgroups.ster"'
eststo CIP_com_byr_sg 

est use `"$output/model_estimation/relax_SUF/parameters/CIP_Commercial_byreasons_largegroups.ster"'
eststo CIP_com_byr_lg

//set cd to avoid conflict on BEA server 
cd `"$output/model_estimation/relax_SUF"'

		#delimit ;
		esttab lr_caid CIP_caid_pr CIP_caid_byr_ag CIP_caid_byr_sg CIP_caid_byr_lg
			   lr_care CIP_care_pr CIP_care_byr_ag CIP_care_byr_sg CIP_care_byr_lg 
			   lr_com CIP_com_pr CIP_com_byr_ag CIP_com_byr_sg CIP_com_byr_lg
			   using "table5_row2.tex", replace substitute(_ \_)
		nonumbers se b(2) se(3) nostar noobs nonotes 
		mtitles("\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{Small \\ group.}" "\shortstack{Large \\ group.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{Small \\ group.}" "\shortstack{Large \\ group.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{All \\ phys.}" "\shortstack{Small \\ group.}" "\shortstack{Large \\ group.}") booktabs mgroups("Medicaid" "Medicare" "Commercial", pattern(1 0 0 0 0 1 0 0 0 0 1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) keep(_cons) 
		coeflabels(_cons "Average CIP");
		
		#delimit cr
		

		
		
		
		
****************************************************
***************** Row 3 ****************************
****************************************************
		
clear all		
set obs 6000
gen y = 1
gen x = 5
gen z = 2

reg y x z, nocons
eststo placeholder 		
	
gen y1 = 1
gen x1 = 5
gen z1 = 2

reg y1 x1 z1, nocons
eststo placeholder1	


local rename _cons taucoef x1 taucoef


//load Medicaid results for model estimations, no reason, no size
use `"$output/model_estimation/relax_SUF/estimations_nosize_noreason_bin.dta"', clear 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicaid_allreasons_allgroups.ster"'
//determine esitmates to be used 
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p99)
keep if pay_type_pooled == 2


//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row3_col2



//load Medicare results for model esimtations, no reason, no size 
use `"$output/model_estimation/relax_SUF/estimations_nosize_noreason_bin.dta"', clear 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicare_allreasons_allgroups.ster"'

//determine esitmates to be used 
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p99)
keep if pay_type_pooled == 1


//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row3_col7


//load Commercial results for model esimtations, no reason, no size 
use `"$output/model_estimation/relax_SUF/estimations_nosize_noreason_bin.dta"', clear 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Commercial_allreasons_allgroups.ster"'

//determine esitmates to be used 
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p99)
keep if pay_type_pooled == 3


//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row3_col12

//set cd to avoid conflict on BEA server 
cd `"$output/model_estimation/relax_SUF"'

local rename _cons taucoef z taucoef
	#delimit;

	esttab placeholder row3_col2 placeholder1 placeholder1 placeholder1 placeholder row3_col7 placeholder1 placeholder1 placeholder1 
	placeholder row3_col12 placeholder1 placeholder1 placeholder1 using "table5_row3.tex", 
	rename(`rename') keep(taucoef) replace 
	coeflabels(taucoef "$\mu^{0}$, all")
	not stats(serror, labels(" ")) nostar b(2) noobs nolines nonumber nomtitles;

	#delimit cr


	
****************************************************
***************** Row 4 ****************************
****************************************************	
local rename_row4 extra_line taucoef1 z taucoef1

//load Medicaid estimates 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicaid_allreasons_allgroups.ster"'
eststo row4_col2

//load Medicare estimates 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicare_allreasons_allgroups.ster"'
eststo row4_col7

//load Commercial estimate 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Commercial_allreasons_allgroups.ster"'
eststo row4_col12

//set cd to avoid conflict on BEA server 
cd `"$output/model_estimation/relax_SUF"'

	#delimit;

	esttab placeholder row4_col2 placeholder1 placeholder1 placeholder1 placeholder row4_col7 placeholder1 placeholder1 placeholder1 
	placeholder row4_col12 placeholder1 placeholder1 placeholder1 using "table5_row4.tex", 
	rename(`rename_row4') keep(taucoef1) replace nonotes eqlabels(none)
	coeflabels(taucoef1 "$\mu^{1}$, all")
	se nostar b(2) noobs nolines nonumber nomtitles;

	#delimit cr


****************************************************
*********** MODEL ESTIMATES BY REASON **************
****************************************************
local reasons `""administrative" "contractual" "coverage" "duplicate" "information""'
	
foreach r of local reasons {
	
	if "`r'" == "administrative" {
		
		local row = 5
		local linerow = 6
		local tlab "Admin."
	}
	
	else if "`r'" == "contractual" {
		
		local row = 7
		local linerow = 8
		local tlab "Contr."
	}
	
	else if "`r'" == "coverage" {
		
		local row = 9
		local linerow = 10
		local tlab "Cov."
	}
	
	else if "`r'" == "duplicate" {
		
		local row = 11
		local linerow = 12
		local tlab "Dup."
	}
	
	else if "`r'" == "information" {
		
		local row = 13
		local linerow = 14
		local tlab "Info."
	}
****************************************************
************** Row 5, 7, 9, 11, 13 *****************
****************************************************
clear all 

//crate placeholder 		
clear all		
set obs 6000
gen y = 1
gen x = 5
gen z = 2

reg y x z, nocons
eststo placeholder 		

//local for renaming coefficients 
local rename_row _cons taucoef

*****************************************
********* Column 3 
//load Medicaid results for model esimtations, reason, no size 
use `"$output/model_estimation/relax_SUF/estimations_nosize_bin.dta"', clear 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicaid_`r'_allgroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 2
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col3


*****************************************
********* Column 4
//load Medicaid results for model esimtations, reason, size 
use `"$output/model_estimation/relax_SUF/estimations_bin.dta"', clear 
keep if size_indicator == 1 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicaid_`r'_smallgroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 2
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col4

*****************************************
********* Column 5
//load Medicaid results for model esimtations, reason, size 
use `"$output/model_estimation/relax_SUF/estimations_bin.dta"', clear 
keep if size_indicator == 2
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicaid_`r'_largegroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 2
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col5



*****************************************
********* Column 8
//load Medicare results for model esimtations, reason, no size 
use `"$output/model_estimation/relax_SUF/estimations_nosize_bin.dta"', clear 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicare_`r'_allgroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 1
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col8


*****************************************
********* Column 9
//load Medicare results for model esimtations, reason, size 
use `"$output/model_estimation/relax_SUF/estimations_bin.dta"', clear 
keep if size_indicator == 1 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicare_`r'_smallgroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 1
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col9

*****************************************
********* Column 10
//load Medicaid results for model esimtations, reason, size 
use `"$output/model_estimation/relax_SUF/estimations_bin.dta"', clear 
keep if size_indicator == 2
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicare_`r'_largegroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 1
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col10



*****************************************
********* Column 13
//load Commercial results for model esimtations, reason, no size 
use `"$output/model_estimation/relax_SUF/estimations_nosize_bin.dta"', clear 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Commercial_`r'_allgroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 3
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col13


*****************************************
********* Column 14
//load Medicaid results for model esimtations, reason, size 
use `"$output/model_estimation/relax_SUF/estimations_bin.dta"', clear 
keep if size_indicator == 1 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Commercial_`r'_smallgroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 3
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col14

*****************************************
********* Column 15
//load Medicaid results for model esimtations, reason, size 
use `"$output/model_estimation/relax_SUF/estimations_bin.dta"', clear 
keep if size_indicator == 2
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Commercial_`r'_largegroups.ster"'

//determine esitmates to be used 
keep if pay_type_pooled == 3
sum claim_cost_se , d
keep if claim_cost_se > 0 & claim_cost_se<r(p95)
keep if visit_code_cat == "`r'"

//computing the standard errors of the mean coefficient using the delta method
matrix varcov = e(V)
forval x = 1/51 { 
	cap drop cov_`x'
	gen cov_`x' = .
	
	forval y = 1/51 {
	
		replace cov_`x' = varcov[`x', `y'] if state == `y'
}
}

gen sum_tmp = .
forval x = 1/51 {
	
	gegen sum_cov_`x' = sum(cov_`x')
	replace sum_tmp = sum_cov_`x' if _n == `x'
	
}
gegen sum_final = sum(sum_tmp)

di sqrt((sum_final/_N^2))
local stnder = round(sqrt((sum_final/_N^2)),0.01)

di `stnder'

//calculating the mean cofficient 
replace claim_cost = claim_cost*(-1)
reg claim_cost
estadd local serror "(`stnder')"
eststo row`row'_col15

//set cd to avoid conflict on BEA server 
cd `"$output/model_estimation/relax_SUF"'

// tabulate the row 
	#delimit;

	esttab placeholder placeholder row`row'_col3 row`row'_col4 row`row'_col5 placeholder placeholder row`row'_col8 row`row'_col9 row`row'_col10 
	placeholder placeholder row`row'_col13 row`row'_col14 row`row'_col15 using "table5_row`row'.tex", 
	rename(`rename_row') keep(taucoef) 
	coeflabels(taucoef "$\mu^{0}$, `tlab' $\rho$")
	replace 
	not stats(serror, labels(" ")) nostar b(2) noobs nolines nonumber nomtitles;

	#delimit cr




	
****************************************************
********* Rows 6, 8, 10, 12 and 14 *****************
****************************************************	
local rename_linerow extra_line taucoef1

*****************************************
********* Column 3
//load Medicaid estimates 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicaid_`r'_allgroups.ster"'
eststo row`linerow'_col3

*****************************************
********* Column 4
//load Medicaid estimates 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicaid_`r'_smallgroups.ster"'
eststo row`linerow'_col4

*****************************************
********* Column 5
//load Medicaid estimates 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicaid_`r'_largegroups.ster"'
eststo row`linerow'_col5


*****************************************
********* Column 8
//load Medicare estimates 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicare_`r'_allgroups.ster"'
eststo row`linerow'_col8

*****************************************
********* Column 9
//load Medicare estimates 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicare_`r'_smallgroups.ster"'
eststo row`linerow'_col9

*****************************************
********* Column 10
//load Medicare estimates 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicare_`r'_largegroups.ster"'
eststo row`linerow'_col10

*****************************************
********* Column 13
//load Medicare estimates 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Commercial_`r'_allgroups.ster"'
eststo row`linerow'_col13

*****************************************
********* Column 14
//load Medicare estimates 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Commercial_`r'_smallgroups.ster"'
eststo row`linerow'_col14

*****************************************
********* Column 15
//load Medicare estimates 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Commercial_`r'_largegroups.ster"'
eststo row`linerow'_col15

//set cd to avoid conflict on BEA server 
cd `"$output/model_estimation/relax_SUF"'

	#delimit;

	esttab placeholder placeholder row`linerow'_col3 row`linerow'_col4 row`linerow'_col5 placeholder placeholder row`linerow'_col8 row`linerow'_col9 row`linerow'_col10 placeholder placeholder row`linerow'_col13 row`linerow'_col14 row`linerow'_col15 using "table5_row`linerow'.tex", 
	rename(`rename_linerow') keep(taucoef1) eqlabels(none)
	coeflabels(taucoef1 "$\mu^{1}$, `tlab' $\rho$")
	replace se nostar b(2) noobs nolines nonumber nomtitles nonotes; 
	#delimit cr


}



***************************************************
************** Rows 15 and 16 *********************
***************************************************
eststo clear 
clear all 
set obs 10 
//placeholder for column 1 
gen placeholder_col_nomodel = 0
estpost tabstat placeholder_col_nomodel
eststo col_nomodelNLL


//load the tau estimates for Medicaid 
est use `"$output/model_estimation/relax_SUF/parameters/taurf_Medicaid.ster"'
gen caid_nom_N = e(N)

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicaid_pooledreasons.ster"'
gen caid_pr_N = e(N)

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicaid_byreasons_allgroups.ster"'
gen caid_all_N = e(N)

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicaid_byreasons_smallgroups.ster"'
gen caid_small_N = e(N)

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicaid_byreasons_largegroups.ster"'
gen caid_large_N = e(N)

//load the tau estimates for Medicare 
est use `"$output/model_estimation/relax_SUF/parameters/taurf_Medicare.ster"'
gen care_nom_N = e(N)

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicare_pooledreasons.ster"'
gen care_pr_N = e(N)

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicare_byreasons_allgroups.ster"'
gen care_all_N = e(N)

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicare_byreasons_smallgroups.ster"'
gen care_small_N = e(N)

est use `"$output/model_estimation/relax_SUF/parameters/tau_Medicare_byreasons_largegroups.ster"'
gen care_large_N = e(N)

//load the tau estimates for Commercial 
est use `"$output/model_estimation/relax_SUF/parameters/taurf_Commercial.ster"'
gen com_nom_N = e(N)

est use `"$output/model_estimation/relax_SUF/parameters/tau_Commercial_pooledreasons.ster"'
gen com_pr_N = e(N)

est use `"$output/model_estimation/relax_SUF/parameters/tau_Commercial_byreasons_allgroups.ster"'
gen com_all_N = e(N)

est use `"$output/model_estimation/relax_SUF/parameters/tau_Commercial_byreasons_smallgroups.ster"'
gen com_small_N = e(N)

est use `"$output/model_estimation/relax_SUF/parameters/tau_Commercial_byreasons_largegroups.ster"'
gen com_large_N = e(N)




//load the tau estimates for Medicaid -- pooled reason 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicaid_allreasons_allgroups.ster"'
gen tau_caid_pr_LL = e(ll)

estpost tabstat caid_nom_N
eststo col1N

estpost tabstat caid_pr_N
eststo col2N

estpost tabstat tau_caid_pr_LL
eststo col2LL

local version `""all" "small" "large""'


//Medicaid Estimations by reason 
foreach v of local version {
	
	//load the tau estimates for Medicaid -- by reason, all groups 
	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicaid_administrative_`v'groups.ster"'
	gen admin_caid_`v'_LL = e(ll)
	
	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicaid_contractual_`v'groups.ster"'
	gen contr_caid_`v'_LL = e(ll)
	
	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicaid_coverage_`v'groups.ster"'
	gen cov_caid_`v'_LL = e(ll)
	
	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicaid_duplicate_`v'groups.ster"'
	gen dup_caid_`v'_LL = e(ll)
	
	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicaid_information_`v'groups.ster"'
	gen info_caid_`v'_LL = e(ll)
	
	gen total_caid_`v'_LL = admin_caid_`v'_LL + contr_caid_`v'_LL + cov_caid_`v'_LL + dup_caid_`v'_LL + info_caid_`v'_LL

}
 
estpost tabstat caid_all_N
eststo col3N

estpost tabstat caid_small_N
eststo col4N

estpost tabstat caid_large_N
eststo col5N

estpost tabstat total_caid_all_LL
eststo col3LL

estpost tabstat total_caid_small_LL
eststo col4LL

estpost tabstat total_caid_large_LL
eststo col5LL

//load the tau estimates for Medicare -- pooled reason 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicare_allreasons_allgroups.ster"'
gen tau_care_pr_LL = e(ll)
 
estpost tabstat care_nom_N
eststo col6N

estpost tabstat care_pr_N
eststo col7N

estpost tabstat tau_care_pr_LL
eststo col7LL

//Medicare Estimations 
foreach v of local version {
	
	//load the tau estimates for Medicare -- by reason, all groups 
	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicare_administrative_`v'groups.ster"'
	gen admin_care_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicare_contractual_`v'groups.ster"'
	gen contr_care_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicare_coverage_`v'groups.ster"'
	gen cov_care_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicare_duplicate_`v'groups.ster"'
	gen dup_care_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Medicare_information_`v'groups.ster"'
	gen info_care_`v'_LL = e(ll)

	gen total_care_`v'_LL = admin_care_`v'_LL + contr_care_`v'_LL + cov_care_`v'_LL + dup_care_`v'_LL + info_care_`v'_LL

}
 
estpost tabstat care_all_N
eststo col8N

estpost tabstat care_small_N
eststo col9N

estpost tabstat care_large_N
eststo col10N

estpost tabstat total_care_all_LL
eststo col8LL

estpost tabstat total_care_small_LL
eststo col9LL

estpost tabstat total_care_large_LL
eststo col10LL

//load the tau estimates for Commercial -- pooled reason 
est use `"$output/model_estimation/relax_SUF/parameters/model_est_Commercial_allreasons_allgroups.ster"'
gen tau_com_pr_LL = e(ll)

estpost tabstat com_nom_N
eststo col11N

estpost tabstat com_pr_N
eststo col12N

estpost tabstat tau_com_pr_LL
eststo col12LL

//Commercial Estimations 
foreach v of local version {
	
	//load the tau estimates for Commercial -- by reason, all groups 
	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Commercial_administrative_`v'groups.ster"'
	gen admin_com_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Commercial_contractual_`v'groups.ster"'
	gen contr_com_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Commercial_coverage_`v'groups.ster"'
	gen cov_com_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Commercial_duplicate_`v'groups.ster"'
	gen dup_com_`v'_LL = e(ll)

	est use `"$output/model_estimation/relax_SUF/parameters/model_est_Commercial_information_`v'groups.ster"'
	gen info_com_`v'_LL = e(ll)

	gen total_com_`v'_LL = admin_com_`v'_LL + contr_com_`v'_LL + cov_com_`v'_LL + dup_com_`v'_LL + info_com_`v'_LL

}
 
estpost tabstat com_all_N
eststo col13N

estpost tabstat com_small_N
eststo col14N

estpost tabstat com_large_N
eststo col15N

estpost tabstat total_com_all_LL
eststo col13LL

estpost tabstat total_com_small_LL
eststo col14LL

estpost tabstat total_com_large_LL
eststo col15LL

//set cd to avoid conflict on BEA server 
cd `"$output/model_estimation/relax_SUF"'

#delimit ;
	esttab col1N col2N col3N col4N col5N col6N col7N col8N col9N col10N col11N col12N col13N col14N col15N 
			using "table5_row15.tex" , cells("mean(fmt(%20.0fc))") 
	rename(caid_nom_N "Number of visits" caid_pr_N "Number of visits" caid_all_N "Number of visits" caid_small_N "Number of visits" caid_large_N "Number of visits" 
			care_nom_N "Number of visits" care_pr_N "Number of visits" care_all_N "Number of visits" care_small_N "Number of visits" care_large_N "Number of visits"
		   com_nom_N "Number of visits" com_pr_N "Number of visits" com_all_N "Number of visits" com_small_N "Number of visits" com_large_N "Number of visits")
	unstack nonumbers booktabs replace substitute(_ \_) noobs;
#delimit cr


#delimit ;
	esttab col_nomodelNLL col2LL col3LL col4LL col5LL col_nomodelNLL col7LL col8LL col9LL col10LL col_nomodelNLL col12LL col13LL col14LL col15LL 
			using "table5_row16.tex", cells("mean(fmt(%20.0fc))") 
	rename(placeholder_col_nomodel "Log Likelihood" tau_caid_pr_LL "Log Likelihood" total_caid_all_LL "Log Likelihood" total_caid_small_LL "Log Likelihood" total_caid_large_LL "Log Likelihood"
		   tau_care_pr_LL "Log Likelihood" total_care_all_LL "Log Likelihood" total_care_small_LL "Log Likelihood" total_care_large_LL "Log Likelihood"
		   tau_com_pr_LL "Log Likelihood" total_com_all_LL "Log Likelihood" total_com_small_LL "Log Likelihood" total_com_large_LL "Log Likelihood")
	unstack nonumbers booktabs replace substitute(_ \_) noobs;
#delimit cr






//Combine the tables into one tables with three panels 
cap prog drop panelcombine
prog define panelcombine
qui {
syntax, use(str asis) paneltitles(str asis) columncount(integer) save(str asis) [CLEANup]
preserve

tokenize `"`paneltitles'"'
//read in loop
local num 1
while "``num''"~="" {
local panel`num'title="``num''"
local num=`num'+1
}


tokenize `use'
//read in loop
local num 1
while "``num''"~="" {
tempfile temp`num'
insheet using "``num''", clear
save `temp`num''
local max = `num'
local num=`num'+1
}

//conditional processing loop
local num 1
while "``num''"~="" {
local panellabel : word `num' of `c(alpha)'
use `temp`num'', clear
	if `num'==1 { //process first panel -- clip bottom
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="\end{tabular}" | v1=="}"
	replace v1 = "[1em]" if v1=="\bottomrule" 
	}
	
	else if `num'==14 { //process final panel -- clip top
	//process header to drop everything until first hline
	drop if v1=="\begin{tabular}{l*{15}{c}}"
	replace v1="[0.5em]" if v1=="[1em]"
	replace v1 = "\midrule" if v1=="\end{tabular}"
		}
		
	if `num'==15 { //process first panel -- clip bottom
	g temp = (v1 == "\midrule")
	replace temp = temp+temp[_n-1] if _n>1
	drop if temp==0
	drop temp
	
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="}" | v1=="\end{tabular}" | v1=="\bottomrule" | v1=="\midrule" 
	}
		
	else if `num'==`max' { //process final panel -- clip top
	//process header to drop everything until first hline
	g temp = (v1 == "\midrule")
	replace temp = temp+temp[_n-1] if _n>1
	drop if temp==0
	drop temp
	drop if v1=="\begin{tabular}{l*{15}{c}}" | v1=="\midrule" 
	replace v1="[0.5em]" if v1=="[1em]"
			}
		
	else if `num'==2 { //process middle panels -- clip top and bottom
	//process header to drop everything until first hline
	g temp = (v1 == "\midrule")
	replace temp = temp+temp[_n-1] if _n>1
	drop if temp==0
	drop temp
	
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="}" | v1=="\end{tabular}"
	//replace v1="[1em]" if v1=="\end{tabular}"
	drop if v1=="\midrule"
	replace v1 = "\midrule \\" if v1=="\bottomrule"
	}
	
	else if `num'== 3 { //process middle panels -- clip top and bottom
	//process header to drop everything until first hline
	
	// drop if v1=="\begin{tabular}{l*{15}{c}}"
	drop if v1=="\begin{tabular}{l*{15}{c}}"
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="\end{tabular}" | v1=="}"
	replace v1 = "" if v1=="\bottomrule"
	drop if v1=="\bottomrule"
	drop if v1=="\midrule"
	}

	forval cline = 5(2)13 {
		else if `num'== `cline' { //process middle panels -- clip top and bottom
		//process header to drop everything until first hline
		
		// drop if v1=="\begin{tabular}{l*{15}{c}}"
		replace v1="[1em]" if v1=="\begin{tabular}{l*{15}{c}}"
		drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
		drop if v1=="\end{tabular}" | v1=="}"
		replace v1 = "" if v1=="\bottomrule"
		drop if v1=="\bottomrule"
		drop if v1=="\midrule"
		}
	}
	forval rline = 4(2)12 {
		else if `num'==`rline' { //process middle panels -- clip top and bottom
		//process header to drop everything until first hline
		
		drop if v1=="\begin{tabular}{l*{15}{c}}"
		replace v1="[0.5em]" if v1=="[1em]"
		drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
		drop if v1=="\end{tabular}" | v1=="}"
		replace v1 = "" if v1=="\bottomrule"
		drop if v1=="\bottomrule"
		drop if v1=="\midrule"
		}
	}
	save `temp`num'', replace
local num=`num'+1
}

use `temp1',clear
local num 2
while "``num''"~="" {
append using `temp`num''
local num=`num'+1
}

outsheet using `save', noname replace noquote


	if "`cleanup'"!="" { //erasure loop
	tokenize `use'
	local num 1
		while "``num''"~="" {
		erase "``num''"
		local num=`num'+1
		}
	}

restore
}
end

cd `"$output/model_estimation/relax_SUF"'
panelcombine, use(table5_row1.tex table5_row2.tex table5_row3.tex table5_row4.tex table5_row5.tex table5_row6.tex table5_row7.tex table5_row8.tex table5_row9.tex table5_row10.tex table5_row11.tex table5_row12.tex table5_row13.tex table5_row14.tex table5_row15.tex table5_row16.tex)  columncount(15) paneltitles("") save(`"$online_appendix/model_relax_SUF.tex"') cleanup 








